<template>
  <div>
    <div class="header">
      <div class="left">商品分类</div>
      <div class="right" @click="$router.push('/search')">
        <div class="search">
          <van-icon name="search" color="#ccc" size=".5rem" />
          <span>双十一爆款5折抢</span>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="left">
        <div
          :class="['left_item', { active: index === activeIndex }]"
          v-for="(item, index) in cates"
          :key="item.id"
          @click="swiperIndex(index)"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="container_right">
        <div class="noCate" v-if="!catesList.length">暂无商品分类</div>
        <div class="rigth_item" @click="goDetail(item.id)" v-for="item in catesList" :key="item.id">
          <img :src="item.pic" alt="" />
          <p>{{item.name}}</p>
        </div>
      </div>
    </div>
    <tabbar></tabbar>
  </div>
</template>

<script>
import { fetchCate, fetchItem } from '_api'
import Tabbar from '../../components/Tabbar.vue'
export default {
  data () {
    return {
      cates: [],
      catesList: [],
      activeIndex: 0 // 高亮的下标
    }
  },
  created () {
    this.fetchCate()
  },
  methods: {
    // 请求分类对应的商品列表
    fetchItem () {
      fetchItem({
        categoryId: this.cates[this.activeIndex].id
      }).then(res => {
        if (res.data.code === 0) {
          this.catesList = res.data.data.result
        }
        if (res.data.code === 700) {
          this.catesList = []
        }
      })
    },
    // 请求分类
    fetchCate () {
      fetchCate().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
        }
        this.fetchItem()
      })
    },
    // 切换高亮
    swiperIndex (index) {
      this.activeIndex = index
      this.fetchItem()
    },
    // 点击去详情

    goDetail (id) {
      this.$router.push(`/detail/${id}`)
    }
  },
  components: { Tabbar }
}
</script>

<style lang="scss" scoped>
.header {
  height: 1.44rem;
  background: #fafafa;
  position: fixed;
  width: 100%;
  left: 0;
  top: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  .left {
    width: 2.4rem;
    height: 100%;
    font-size: 0.48rem;
    color: #333;
    line-height: 1.44rem;
    text-align: center;
  }
  .right {
    flex: 1;
  }
  .search {
    box-sizing: border-box;
    width: 6.96rem;
    height: 0.853333rem;
    background: #f7f7f7;
    border-radius: 0.266667rem;
    display: flex;
    align-items: center;
    font-size: 0.373333rem;
    color: #ccc;
    padding-left: 0.133333rem;
    span {
      margin-left: 0.133333rem;
    }
  }
}

.container {
  position: fixed;
  left: 0;
  top: 1.44rem;
  bottom: 1.2rem;
  display: flex;
  right: 0;
  background: #ffff;
  .left {
    width: 2.4rem;
    overflow-y: auto;

    &_item {
      height: 1.333333rem;
      padding: 0 0.333333rem;
      line-height: 1.333333rem;
      text-align: center;
      background-color: #f2f2f2;
      font-size: 0.373333rem;
      overflow: hidden;
      position: relative;
    }
  }
}
.left_item.active {
  background-color: #fff;
  font-weight: 700;
  &::before {
    content: "";
    position: absolute;
    width: 0.106667rem;
    height: 0.24rem;
    background-color: #ffcc00;
    border-radius: 0.053333rem;
    left: 0.133333rem;
    top: 0.546667rem;
  }
}
.container_right {
  width: 7.6rem;
  height: 100%;
  padding: 0.2rem;
  overflow-y: auto;
}
.rigth_item {
  display: inline-block;
  width: 1.68rem;
  height: 2.16rem;
  margin: 0 0.88rem 0.4rem 0;
  img {
    width: 100%;
    height: 1.68rem;
  }
  p {
    font-size: .266667rem;
    text-align: center;
    white-space: nowrap;
    text-overflow: ellipsis;
    padding-top: .066667rem;
    overflow: hidden;
    color: #666;
  }
}
.rigth_item:nth-child(3n) {
  margin-right: 0;
}
.noCate{
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: .453333rem;
  color: #333;
}
</style>
